package com.pet_service.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pet_service.entity.FosterOrder;
import com.pet_service.entity.Orders;
import com.pet_service.mapper.FosterOrderMapper;
import com.pet_service.mapper.OrderHomeServiceMapper;
import com.pet_service.result.Result;
import com.pet_service.service.FosterOrderService;
import com.pet_service.vo.PageBean;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author 邹全伸
 * @version 1.0
 * @date 2025-09-25 17:03
 */
@Slf4j
@RestController//作用是将Controller组件注册为Bean,@Controller和@ResponseBody的组合注解
@RequestMapping("/foster")
@Tag(name = "寄养接口")
public class FosterController {
    @Resource
    private FosterOrderService fosterOrderService;

    // 根据订单id和医院id查询寄养订单
    @GetMapping("/order")
    @PreAuthorize("hasAuthority('Service:foster:order:view')")
    public Result getOrder(@RequestParam Integer order_id, @RequestParam Integer hospital_id) {
        log.info("根据订单id和医院id查询寄养订单,参数:{},{}", order_id, hospital_id);

        QueryWrapper<FosterOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", order_id)
                .eq("hospital_id", hospital_id);

        FosterOrder fosterOrder = fosterOrderService.getOne(queryWrapper);

        if (fosterOrder != null) {
            return Result.ok(fosterOrder);
        }

        return Result.fail("未找到相关寄养订单");
    }

    // 分页查询寄养订单
    @PostMapping("/order/list")
    @PreAuthorize("hasAuthority('Service:foster:order:view')")
    public Result orderList(@RequestBody PageBean pageBean) {
        log.info("分页查询寄养订单,参数:{}", pageBean);

        Page<FosterOrder> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        QueryWrapper<FosterOrder> queryWrapper = new QueryWrapper<>();

        // 可以添加更多查询条件
        if (pageBean.getQuery() != null && !pageBean.getQuery().trim().isEmpty()) {
            queryWrapper.like("order_no", pageBean.getQuery().trim())
                    .or()
                    .like("pet_name", pageBean.getQuery().trim());
        }

        IPage<FosterOrder> iPage = fosterOrderService.page(page, queryWrapper);
        return Result.ok(iPage);
    }

    // 添加寄养订单
    @PostMapping("/order/add")
    @PreAuthorize("hasAuthority('Service:foster:order:add')")
    public Result addOrder(@RequestBody FosterOrder fosterOrder) {
        log.info("添加寄养订单,参数:{}", fosterOrder);

        // 检查订单是否存在
        if (fosterOrderService.count(new QueryWrapper<FosterOrder>()
                .eq("order_id", fosterOrder.getOrder_id())) > 0) {
            return Result.fail("订单已存在");
        }

        boolean success = fosterOrderService.save(fosterOrder);
        return success ? Result.ok() : Result.fail();
    }

    // 修改寄养订单
    @PostMapping("/order/edit")
    @PreAuthorize("hasAuthority('Service:foster:order:edit')")
    public Result editOrder(@RequestBody FosterOrder fosterOrder) {
        log.info("修改寄养订单,参数:{}", fosterOrder);

        boolean success = fosterOrderService.updateById(fosterOrder);
        return success ? Result.ok() : Result.fail();
    }

    // 删除寄养订单
    @DeleteMapping("/order/delete/{id}")
    @PreAuthorize("hasAuthority('Service:foster:order:delete')")
    public Result deleteOrder(@PathVariable Integer id) {
        log.info("删除寄养订单,参数:{}", id);

        boolean success = fosterOrderService.removeById(id);
        return success ? Result.ok() : Result.fail();
    }

    // 查询寄养订单状态
    @GetMapping("/order/status/{id}")
    @PreAuthorize("hasAuthority('Service:foster:order:view')")
    public Result getOrderStatus(@PathVariable Integer id) {
        log.info("查询寄养订单状态,参数:{}", id);

        FosterOrder fosterOrder = fosterOrderService.getById(id);
        if (fosterOrder != null) {
            return Result.ok(fosterOrder.getStatus());
        }

        return Result.fail("订单不存在");
    }

}
